#
# TrashSourceViewItem.py
#
# This work is released under the GNU GPL, version 2 or later.
#

from SourceView import *
from PlaylistDrag import *
from TrackDrag import *
#from PodTrackDrag import *
#from PodPlaylistDrag import *

#
# "Trash"
#
class TrashSourceViewItem(SourceViewItem):
	def __init__(self,container):
		SourceViewItem.__init__(self,container,i18n('Trash'))
		self.wantsTrackDrags = True
		self.wantsPlaylistDrags = True
		'''
		self.wantsDellPlaylistDrags = True
		self.wantsDellTrackDrags = True
		self.wantsPodPlaylistDrags = True
		self.wantsPodTrackDrags = True
		'''
		self.wantsEncoderDrags = True
		self.canEmpty = True
	def makeKey(self):
		return 'L'

	def iconFileName(self): return "sourcetrash.png"
	
	def select(self,container):
		container.emit(PYSIGNAL('selectTrash'),(None,None))
	
	def buildPopup(self,mainWindow):
		self.popup = QPopupMenu()
		self.emptyid = self.popup.insertItem(i18n("Empty &Trash"),mainWindow.slotEmptyTrash)
	
	def loadPopup(self):
		self.popup.setItemEnabled(self.emptyid,self.trash.hasTracks())

	#
	# handle drags to the trash
	#
	# if drag is playlist, remove the playlist
	# if drag is tracks:
	#    if source is playlist, remove tracks from playlist
	#    if source is library, remove tracks from all playlists and put in trash
	#    if source is MP3 player, remove tracks or playlists from player
	#
	def dropped(self,event):
		if PlaylistDrag.canDecode(event):
			drag  = PlaylistDrag.decode(event)
			playlist = self.library.playlistWithName(drag['Playlist'])
			if playlist:
				self.library.removePlaylist(playlist)
		elif TrackDrag.canDecode(event):
			drag = TrackDrag.decode(event)
			#print "trash got",drag
			playlist = self.library.playlistWithName(drag['Playlist'])
			trackIDs = drag['TrackIDs']
			if not playlist == self.trash:
				if playlist == self.libraryPlaylist:
					self.trash.addTrackIDs(trackIDs)
					self.library.removeTrackIDsFromAllPlaylists(trackIDs)
				else:
					playlist.removeTrackIDs(trackIDs)
		elif EncoderTrackDrag.canDecode(event):
			drag = LEncoderTrackDrag.decode(event)
			trackIDs = drag['TrackIDs']
			e = EncoderLibrary.singleton()
			e.removeTrackIDsFromAllPlaylists(trackIDs)
			e.killTrackIDs(trackIDs)	
		'''
		elif PodPlaylistDrag.canDecode(event):
			drag = PodPlaylistDrag.decode(event)
			lib = PodLibrary().singleton()
			playlist = lib.playlistWithName(drag['Playlist'])
			if playlist:
				from PodManager import PodManager
				PodManager.singleton().removePlaylist(playlist)
		elif PodTrackDrag.canDecode(event):
			drag = PodTrackDrag.decode(event)
			lib = PodLibrary.singleton()
			playlist = lib.playlistWithName(drag['Playlist'])
			trackIDs = drag['TrackIDs']
			if playlist:
				from PodManager import LMusicPodManager
				PodManager.singleton().removeTrackIDsFromPlaylist(playlist,trackIDs)
		'''
	
